package com.demo.core.dao;

import com.demo.base.model.UserInfo;
import com.demo.base.model.user.Actions;
import com.demo.base.model.user.RoleAction;
import com.demo.base.model.user.Roles;
import com.demo.base.model.user.UserRole;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jiandandia on 2017/4/6.
 */

@Repository
public class ActionsDao extends BaseDao{

    public Roles saveRole(Roles roles) throws Exception{
        return saveEntity(roles);
    }

    public void deleteRoleById(String roleId) throws Exception{
        Session s = getSession();
        try {
            s.delete(s.get(Roles.class, roleId));
        }catch (Exception e){
            throw new Exception(e);
        }
    }
    public List<Actions> getRoleActions(String roleId) {
        StringBuilder hql = new StringBuilder(" from Actions a where exists(");
        hql.append("select ra.id from RoleAction ra where a.id=ra.actionId and ra.roleId=?");
        hql.append(")");
        return hFind(hql.toString(), Actions.class, new String[]{roleId});
    }

    public void saveRoleActions(String roleId, String[] actionIds) {
        Session s = getSession();
        try {
            for (String actionId : actionIds) {
                RoleAction ra = new RoleAction();
                ra.setRoleId(roleId);
                ra.setActionId(actionId);
                s.save(ra);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void deleteRoleActions(String roleId, String[] actionIds) {
        StringBuilder hql = new StringBuilder(" delete from RoleAction where roleId=? and actionId in (");
        List<String> params = new ArrayList<String>();
        params.add(roleId);
        for (int i = 0, ln = actionIds.length; i < ln; i++) {
            if (i > 0)
                hql.append(",");
            hql.append("?");
            params.add(actionIds[i]);
        }
        hql.append(")");
        hExecuteUpdate(hql.toString(), params.toArray(new String[]{}));
    }

    public List<UserInfo> getRoleUsers(String roleId) {
        StringBuilder hql = new StringBuilder(" from UserInfo r where exists(");
        hql.append("select ur.id from UserRole ur where r.id=ur.userid and ur.roleId=?");
        hql.append(")");
        return hFind(hql.toString(), UserInfo.class, new String[]{roleId});
    }

    public void newRoleUsers(String roleId, String[] userIds) {
        Session s = getSession();
        try {
            for (String userId : userIds) {
                UserRole ur = new UserRole();
                ur.setUserid(userId);
                ur.setRoleId(roleId);
                s.save(ur);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void deleteRoleUsers(String roleId, String[] userIds){
        StringBuilder hql = new StringBuilder(" delete from UserRole where roleId=? and userid in (");
        List<String> params = new ArrayList<String>();
        params.add(roleId);
        for (int i = 0, ln = userIds.length; i < ln; i++) {
            if (i > 0){
                hql.append(",");
            }else{
                hql.append("?");
                params.add(userIds[i]);
            }
        }
        hql.append(")");
        hExecuteUpdate(hql.toString(), params.toArray(new String[]{}));
    }
}
